<template>
  <div>
    <div class="blockLeft">
      <span class="aSpan">{{ptitle}}</span>
    </div>
    <div :class="pline === 1?'blockRightLong':'blockRight'">
      <span v-if="preadonly"
            class="aSpan">{{content}}</span>
      <input v-else
             type="number"
             v-model="realValue"
             @blur="blurAction"
             @input="inputAction"
             @change="changeAction"
             class="aInput">
    </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
export default {
  name: 'bpmCBlocknumber',
  model: {
    prop: 'pcontent',
    event: 'changed'
  },
  props: {
    // 只读/可编辑,
    preadonly: {
      type: Boolean,
      default: false
    },
    // 可能占一整行或行的一半
    pline: {
      type: Number,
      default: 2
    },
    ptitle: {
      type: String,
      default: ' '
    },
    pcontent: {
      type: [String, Number],
      default: ''
    },
    validateExp: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  data: function () {
    return {
      realValue: isNaN(this.pcontent) || (this.pcontent + '' === '') ? '0' : this.pcontent + '',
      pover: false
    }
  },
  computed: {
    ...mapState(['otherI18n', 'pageType', 'pageTypeEnum']),
    content () {
      return this.toHaveComma(this.realValue)
    }
  },
  watch: {
    pcontent (to) {
      if (isNaN(to)) {
        this.realValue = '0'
      } else {
        this.realValue = to + ''
      }
    },
    realValue (to) {
      if (isNaN(to)) {
        this.realValue = '0'
      }
    }
  },
  methods: {
    toHaveComma (value) {
      if (!value) {
        return '0'
      }
      // 判断是否是纯数字，如果不是不进行加点运算
      if (!this.validate(value)) { return value }
      value += ''
      var intPart = Math.floor(Number(value)) // 获取整数部分
      var intPartFormat = intPart.toString().replace(/(\d)(?=(?:\d{3})+$)/g, '$1,') // 将整数部分逢三一断
      if (this.validateExp && this.validateExp.positiveInteger) {
        return intPartFormat
      }
      var floatPart = '.00' // 预定义小数部分
      var value2Array = value.split('.')
      // =2表示数据有小数位
      if (value2Array.length === 2) {
        floatPart = value2Array[1].toString() // 拿到小数部分
        if (floatPart.length === 1) { // 补0,实际上用不着
          return intPartFormat + '.' + floatPart + '0'
        } else {
          return intPartFormat + '.' + floatPart
        }
      } else {
        return intPartFormat + floatPart
      }
    },
    // keyPressAction (event) {
    //   let flag = /[\d]/.test(String.fromCharCode(event.keyCode))
    //   console.log('key pressed', flag)
    //   return flag
    // },
    blurAction (event) {
      console.log('number blurrrred....', this.realValue)
      if (isNaN(this.realValue) || this.realValue + '' === '') {
        this.realValue = '0'
      } else if (this.validateExp.positiveDecimal) {
        this.realValue = parseFloat(this.realValue).toFixed(2)
      }
      // else if (this.validateExp.positiveInteger) {
      //   this.realValue = parseFloat(this.realValue).toFixed(2)
      // }
      if (!this.validateExp || !this.validateExp.flag || !this.validateExp.require) {
      } else {
        if (this.pageType === this.pageTypeEnum.approveFee) {

        } else if (!this.realValue || parseFloat(this.realValue) === 0) {
          this.$message({
            type: 'warning',
            message: this.ptitle + this.otherI18n.cannotNull
          })
        }
      }
      this.$emit('changed', this.realValue)
      this.$emit('valueChanged', this.realValue)
    },
    inputAction (event) {
      // console.log(event, this.realValue)
      if (!this.validateExp || !this.validateExp.flag) return
      if (this.validateExp.positiveInteger) {
        this.realValue = parseInt(this.realValue) + ''
      }
      // if (this.validateExp.positiveDecimal) {
      //   this.realValue = parseFloat(this.realValue) + ''
      // }
      if (this.validateExp.maxlength && this.validateExp.maxlength > 0) {
        this.realValue = this.realValue.slice(0, this.validateExp.maxlength)
      }
    },
    changeAction () {
      // this.$emit('changed', this.realValue)
      // this.$emit('valueChanged', this.realValue)
    },
    // 正则表达式
    validate (value) {
      let reg = /^(\+)?\d+(\.\d+)?$/
      // var reg = new RegExp(vali, 'g')
      return reg.test(value)
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
  .blockLeft {
    background-color: #f4f4f4;
    float: left;
    text-align: center;
    width: 140px;
    min-height: 26px;
    border-left: 1px solid #e3e3e3;
    border-right: 1px solid #e3e3e3;
  }

  .blockRight {
    background-color: #eff3f6;
    float: left;
    padding-left: 8px;
    min-height: 26px;
    width: 340px;
    min-width: 340px;
    text-align: left;
  }

  .blockRightLong {
    background-color: #eff3f6;
    float: left;
    padding-left: 8px;
    min-height: 26px;
    width: 830px;
    min-width: 830px;
    text-align: left;
  }

  .aSpan {
    display: block;
    margin-top: 4px;
  }

  .aInput {
    width: 240px;
    margin-top: 4px;
  }
</style>
